package com.phiture.erp.basic.web.controller.admin;

import com.phiture.erp.basic.core.pojo.vo.ErpUserProcessMappingPageReqVO;
import com.phiture.erp.basic.core.pojo.vo.ErpUserProcessMappingSaveReqVO;
import com.phiture.erp.basic.core.service.ErpUserProcessMappingService;
import com.phiture.erp.basic.dal.entity.ErpUserProcessMappingDO;
import com.phiture.erp.basic.dal.pojo.vo.ErpUserProcessMappingRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 人员工序对照")
@RestController
@RequestMapping("/erp/user-process-mapping")
@Validated
public class ErpUserProcessMappingController {

    @Resource
    private ErpUserProcessMappingService userProcessMappingService;

    @PostMapping("/create")
    @Operation(summary = "创建人员工序对照")
    @PreAuthorize("@ss.hasPermission('erp:user-process-mapping:create')")
    public CommonResult<Long> createUserProcessMapping(@Valid @RequestBody ErpUserProcessMappingSaveReqVO createReqVO) {
        return success(userProcessMappingService.createUserProcessMapping(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新人员工序对照")
    @PreAuthorize("@ss.hasPermission('erp:user-process-mapping:update')")
    public CommonResult<Boolean> updateUserProcessMapping(@Valid @RequestBody ErpUserProcessMappingSaveReqVO updateReqVO) {
        userProcessMappingService.updateUserProcessMapping(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除人员工序对照")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:user-process-mapping:delete')")
    public CommonResult<Boolean> deleteUserProcessMapping(@RequestParam("id") Long id) {
        userProcessMappingService.deleteUserProcessMapping(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得人员工序对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:user-process-mapping:query')")
    public CommonResult<ErpUserProcessMappingRespVO> getUserProcessMapping(@RequestParam("id") Long id) {
        ErpUserProcessMappingDO userProcessMapping = userProcessMappingService.getUserProcessMapping(id);
        return success(BeanUtils.toBean(userProcessMapping, ErpUserProcessMappingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得人员工序对照分页")
    @PreAuthorize("@ss.hasPermission('erp:user-process-mapping:query')")
    public CommonResult<PageResult<ErpUserProcessMappingRespVO>> getUserProcessMappingPage(@Valid ErpUserProcessMappingPageReqVO pageReqVO) {
        PageResult<ErpUserProcessMappingRespVO> pageResult = userProcessMappingService.getUserProcessMappingPage(pageReqVO);


        return success(BeanUtils.toBean(pageResult, ErpUserProcessMappingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出人员工序对照 Excel")
    @PreAuthorize("@ss.hasPermission('erp:user-process-mapping:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserProcessMappingExcel(@Valid ErpUserProcessMappingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpUserProcessMappingRespVO> list = userProcessMappingService.getUserProcessMappingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "人员工序对照.xls", "数据", ErpUserProcessMappingRespVO.class,
                        BeanUtils.toBean(list, ErpUserProcessMappingRespVO.class));
    }

}